package 数据结构.链表;

import java.util.*;



//Collections.reverse(arrayList);
class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList arrayList = new ArrayList();
        while(listNode != null){
            arrayList.add(listNode.val);
            listNode = listNode.next;
        }
        Collections.reverse(arrayList);
        return arrayList;
    }
}

//递归
class Solution2 {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList arrayList = new ArrayList();
        reverse(arrayList,listNode);
        return arrayList;
    }

    public void reverse(List list, ListNode listNode){
        if(listNode!=null){
            reverse(list,listNode.next);
            list.add(listNode.val);
        }
    }
}

//栈
class Solution3 {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList arrayList = new ArrayList();
        Stack stack = new Stack();
        while (listNode!=null){
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        while (!stack.isEmpty()){
            arrayList.add(stack.pop());
        }
        return arrayList;
    }
}